package lpl.com.xyd.test.manage.controller;

import lpl.com.xyd.test.manage.common.utils.ContentType;
import lpl.com.xyd.test.manage.facade.MenuManageFacade;
import lpl.com.xyd.test.manage.form.LoginForm;
import lpl.com.xyd.test.manage.info.MenuInfo;
import lpl.com.xyd.test.manage.order.MenuQueryOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户登录
 * @Author mingliang
 * @Date 2018-01-31 14:31
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    private MenuManageFacade menuManageFacade;

    private static String classPath = Thread.currentThread().getContextClassLoader().getResource("").getPath()+"\\templates\\";

    @RequestMapping(value = "/login",method = RequestMethod.GET,produces = {ContentType.APPLICATION_JSON_UTF_8,
            ContentType.TEXT_XML_UTF_8 })
    public ModelAndView login(ModelAndView modelAndView, ModelMap modelMap, LoginForm form){
        // 1、验证登录信息，然后获取用户权限下的菜单列表
        // 2、初始化查询条件
        MenuQueryOrder order = new MenuQueryOrder();
        order.setSystemCode(form.getSystemCode());
        // 3、将菜单列表数据返回到前端
        List<MenuInfo> menuInfos = menuManageFacade.getMenuInfos(order);
        modelMap.put("test","测试");
        modelMap.put("menuList",menuInfos);
        modelMap.put("userName","lpl@xyd.cn");
        modelAndView.setViewName("home");
        return modelAndView;
    }

    /**
     * 点击菜单跳转对应的页面
     * @param
     * @param viewUrl
     * @return
     */
    @RequestMapping(value = "/{viewUrl}",method = RequestMethod.GET,produces = {ContentType.APPLICATION_JSON_UTF_8,
            ContentType.TEXT_XML_UTF_8 })
    public ModelAndView main( @PathVariable("viewUrl") String viewUrl, HttpServletRequest request, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(viewUrl.replaceAll("-","/"));
        return modelAndView;
    }
}
